/*
* @(#)DemoService.java 1.0 Sep 5, 2013
*
* Copyright @2011-2011 Gopay Information and Technology Co., Ltd.
* Sanyunqiao, Chaoyang, Beijing, 100027, China.
* All Rights Reserved.
*
* This software is the confidential and proprietary information of
* Gopay Information and technology Co., Ltd. ("Confidential Information").
* You shall not disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into with Gopay.
*/
package com.neo.debade.weixin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neo.debade.base.service.impl.BaseServiceImpl;
import com.neo.debade.util.RandomUtil;
import com.neo.debade.weixin.service.IWeixinService;
import com.neo.debade.weixin.util.WeixinUtil;
import com.neo.debade.weixin.util.WeixinUtil.MsgType;

/**
 *
 * @author zhong_xin
 * @version 1.0 Sep 5, 2013
 */
//@Service(value="demoService")
public class WeixinService extends BaseServiceImpl implements IWeixinService {

	public WeixinService() {
	}
	
	@Override
	public String processMessage(Map<String, String> map) {
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("FromUserName", map.get("ToUserName"));
		retMap.put("ToUserName", map.get("FromUserName"));
		
		Map<String, Object> tempMap = null;
		String content = map.get("Content").trim();
		
		switch (MsgType.valueOf(map.get("MsgType"))) {
		case text:
			tempMap = processTextMessage(content);
			break;
		case image:
			tempMap = processImageMessage(content);
			break;
		case location:
			tempMap = processLocationMessage(content);
			break;
		case link:
			tempMap = processLinkMessage(content);
			break;
		case event:
			tempMap = processEventMessage(map.get("Event"), map.get("FromUserName"));
			break;
		default:
			break;
		}
		if (tempMap != null) {
			retMap.putAll(tempMap);
			return WeixinUtil.buildResponse(retMap);
		}
		return "";
	}
	
	
	private Map<String, Object> processImageMessage(String content) {
		// to impl
		return null;
	}
	private Map<String, Object> processLocationMessage(String content) {
		// to impl
		return null;
	}
	private Map<String, Object> processLinkMessage(String content) {
		// to impl
		return null;
	}
	
	private Map<String, Object> processEventMessage(String event, String uid) {
		if ("subscribe".equals(event)) {
			logger.info("*********** recv event subscribe ****************");
			return processSubscribe(uid);
		} else {
			try {
				jdbcTemplate.execute("delete from card where cardno = '" + uid + "')");
			} catch (Throwable t) {
				t.printStackTrace();
			}
		}
		return null;
	}

	public Map<String,Object> processSubscribe(String uid){
		try {
			jdbcTemplate.execute("insert into card values ('" + RandomUtil.randomStr(16) + "', '" + uid + "')");
		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("MsgType", MsgType.news.toString());
		logger.info("图文信息内容");
		List<Map<String, Object>> articleList = new ArrayList<Map<String, Object>>();
		Map<String, Object> item = new HashMap<String, Object>();
		item.put("Title", "使用说明，请回复:");
		item.put("Description", "点击访问微站点");
		item.put("Url", "http://debade.duapp.com/");
		item.put("PicUrl", "https://res.wx.qq.com/mpres/htmledition/images/new/logo17e4f4.png");
		articleList.add(item);
		map.put("articles", articleList);
		return map;
	}
	
	/**
	 * @by zhong_xin - Jun 4, 2013 3:23:58 PM
	 */
	private Map<String, Object> processTextMessage(String content) {
		logger.info("*********** recv text ****************");
		Map<String, Object> map = new HashMap<String, Object>();
		if ("mp3".equalsIgnoreCase(content)) {
				logger.info("音乐信息内容");
				map.put("MsgType", MsgType.music.toString());
				map.put("Title", "致青春");
				map.put("Description", "王菲");
				map.put("MusicUrl", "http://zhangmenshiting.baidu.com/data2/music/42911424/4015334072000320.mp3?xcode=16046e934dc5058eff69b23052dbaf6e2a2585502f36e574");
				map.put("HQMusicUrl", "http://zhangmenshiting.baidu.com/data2/music/42911424/4015334072000320.mp3?xcode=16046e934dc5058eff69b23052dbaf6e2a2585502f36e574");
		} else if ("h".equalsIgnoreCase(content) || "help".equalsIgnoreCase(content) || "?".equals(content)) {
			map.put("MsgType", MsgType.news.toString());
			logger.info("图文信息内容");
			List<Map<String, Object>> articleList = new ArrayList<Map<String, Object>>();
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("Title", "使用说明，请回复:");
			item.put("Description", "点击访问微站点");
			item.put("Url", "http://debade.duapp.com/");
			item.put("PicUrl", "https://res.wx.qq.com/mpres/htmledition/images/new/logo17e4f4.png");
			articleList.add(item);
			map.put("articles", articleList);
		} 
		return map;
	}
	
	/*@Resource(name="baseDAO")
	private IBaseDAO baseDAO;
	
	@Transactional(readOnly = false, rollbackFor = Throwable.class)
	@Override
	public void saveCard(Object card) {
		baseDAO.save(card);
	}
*/
	
	
}
